package net.hzlj.platform.enterprise.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.listener.DialogUIListener;

import net.hzlj.platform.enterprise.beans.UserBean;
import net.hzlj.platform.enterprise.common.Constant;
import net.hzlj.platform.enterprise.common.EventBusParameter;
import net.hzlj.platform.enterprise.helper.ToastHelper;
import net.hzlj.rl.zhangjiajie.R;
import net.hzlj.platform.enterprise.utils.AppPreferencesUtils;
import net.hzlj.platform.enterprise.utils.PreferencesUtils;
import net.hzlj.platform.enterprise.utils.StringUtils;
import net.hzlj.platform.enterprise.widght.TitleBar;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PersonalInformationActivity extends BaseActivity {

    private static final String TAG = PersonalInformationActivity.class.getSimpleName();

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.header_ImageView)
    ImageView mHeaderImageView;
    @BindView(R.id.name_textView)
    TextView mNameTextView;
    @BindView(R.id.account_textView)
    TextView mAccountTextView;
    @BindView(R.id.sex_textView)
    TextView mSexTextView;
    @BindView(R.id.phone_textView)
    TextView mPhoneTextView;
    @BindView(R.id.organ_textView)
    TextView mOrganTextView;
    @BindView(R.id.correctBegin_textView)
    TextView mCorrectBeginTextView;
    @BindView(R.id.solveDate_textView)
    TextView mSolveDateTextView;
    @BindView(R.id.manageGrade_textView)
    TextView mManageGradeTextView;
    @BindView(R.id.correctType_textView)
    TextView mCorrectTypeTextView;
    @BindView(R.id.change_password_button)
    LinearLayout mChangePasswordButton;
    @BindView(R.id.fileNumber_textView)
    TextView mFileNumberTextView;
    @BindView(R.id.manageGrade_button)
    LinearLayout mManageGradeButton;


    private String mHeaderUrl = "";
    private UserBean mUserBean;
    private String mManagerGrade = "1";


    private MyHandler<PersonalInformationActivity> mHandler;

    @Override
    protected void setRootView() {
        super.setRootView();
        setContentView(R.layout.activity_personal_information);
    }

    @Override
    protected void initTitleBar() {
        super.initTitleBar();
        mHandler = new MyHandler<PersonalInformationActivity>(this);
        mTitleBar.bindActivity(this);
        mTitleBar.setOnMainLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }, "返回", 0, View.VISIBLE);
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {

    }

    @Override
    protected void onEventRunBackground(EventBusParameter parameter) {
        super.onEventRunBackground(parameter);
        if (EventBusParameter.ACTION_UPDATE_USER.equals(parameter.action)){
            updateData();
        }
    }

    @Override
    protected void initData() {
        super.initData();
        updateData();
    }


    private void updateData(){
        mUserBean = AppPreferencesUtils.getObject(this, PreferencesUtils.KEY_USER_INFO, UserBean.class);
        if (mUserBean != null) {
            mNameTextView.setText(mUserBean.getName());
            mAccountTextView.setText(mUserBean.getIdentityNO());

            mSexTextView.setText(mUserBean.getSex());
            mPhoneTextView.setText(mUserBean.getTelephone());
            mOrganTextView.setText(mUserBean.getRegistered());

            mFileNumberTextView.setText(mUserBean.getFileNumber());

            mCorrectBeginTextView.setText(mUserBean.getCorrectBegin());
            mSolveDateTextView.setText(mUserBean.getSolveDate());

            mManageGradeTextView.setText(mUserBean.getManageGradeDescription());
            mCorrectTypeTextView.setText(mUserBean.getCorrectType());
            mHeaderUrl = mUserBean.getUploadUrl();
//            ImageLoader.getInstance().displayImage(mHeaderUrl,mHeaderImageView, ImageLoaderHelper.getDisplayImageOptions());
            Glide.with(this).load(mHeaderUrl)
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(mHeaderImageView);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @OnClick({R.id.change_password_button, R.id.header_ImageView})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.change_password_button:
                //修改密码
                startActivity(new Intent(PersonalInformationActivity.this, ChangePasswordActivity.class));
                break;
            case R.id.header_ImageView:
                if (StringUtils.isNotEmpty(mHeaderUrl) && (mHeaderUrl.contains("http://") || mHeaderUrl.contains("https://"))) {
                    ArrayList<String> mChoosePhotos = new ArrayList<String>();
                    //BootApp.USER_BEAN.getUploadUrl()
                    mChoosePhotos.add(mHeaderUrl);
                    Intent intent = new Intent(this, PreviewPhotoActivity.class);
                    intent.putStringArrayListExtra("images", mChoosePhotos);
                    intent.putExtra("position", 0);
                    startActivity(intent);
                }
                break;
            case R.id.manageGrade_button:
                //管理登记
                String description = "";
                if ("1".equals(mUserBean.getManageGrade())){
                    description = getString(R.string.manage_grade_general);
                }else if("2".equals(mUserBean.getManageGrade())){
                    description = getString(R.string.manage_grade_loose);
                }else if("3".equals(mUserBean.getManageGrade())){
                    description = getString(R.string.manage_grade_strict);
                }
                Log.i(TAG,description);
                if (StringUtils.isNotEmpty(description)){
                    TextView textView = new TextView(PersonalInformationActivity.this);
                    int width = (int)(Constant.EXACT_SCREEN_WIDTH * 0.7);
                    textView.setWidth(width);
                    textView.setLineSpacing(6f,1.2f);
                    textView.setPadding(20,20,20,20);
                    textView.setText(Html.fromHtml(description));
                    DialogUIUtils.showCustomAlert(PersonalInformationActivity.this, textView).show();
                }
                break;
        }
    }
}
